package com.everflourish.yeah100.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.entity.clazz.ClassManage;
import com.everflourish.yeah100.entity.marking.Student;
import com.everflourish.yeah100.utils.Yeah100;
import com.everflourish.yeah100.utils.constant.Constant;
import com.everflourish.yeah100.utils.constant.RoleEnum;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 班级管理请求接口封装
 *
 * @author pf
 * @date 2015-1-14
 */
public class ClassManageRequest extends BaseRequest {
    private static ClassManageRequest mRequest;

    private ClassManageRequest() {
    }

    public static ClassManageRequest getInstance() {
        if (mRequest == null) {
            mRequest = new ClassManageRequest();
        }
        return mRequest;
    }

    /**
     * 班级列表请求
     *
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest classListRequest(Listener<JSONObject> listener,
                                              ErrorListener errorListener) {
        String url = "";
        if (Yeah100.mRoleEnum == RoleEnum.TEACHER) {
            url = URL_TEACHER_CLASS_LIST;
        } else {
            url = URL_STUDENT_CLASS_LIST;
        }
        JsonObjectRequest request = new JsonObjectRequest(Method.GET, url,
                null, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 班级信息获取
     *
     * @param classId
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest classInfoRequest(String classId,
                                              Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_GET + "/" + classId, null, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 删除班级请求 对象：老师
     *
     * @param classId       班级id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest unfollowClassRequest(String classId,
                                                  Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_UNFOLLOW + "/" + classId, null, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 删除班级请求 对象：学生
     *
     * @param classId       班级id
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest moveClassRequest(String classId,
                                              Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_STUDENT_REMOVE + "/" + classId, null, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 创建班级
     *
     * @param className     班级名称
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest createClassRequest(String className,
                                                Listener<JSONObject> listener, ErrorListener errorListener) {
        String encodeClassName = "";
        try {
            encodeClassName = URLEncoder.encode(className, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_CREATE + "/" + encodeClassName, null, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        return request;
    }

    /**
     * 更新班级
     *
     * @param clazz         班级信息
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest updateClassRequest(ClassManage clazz,
                                                Listener<JSONObject> listener, ErrorListener errorListener) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", clazz.getId());
        map.put("name", clazz.getName());
        // map.put("sharingNo", clazz.getSharingNo());
        JSONObject jsonObject = new JSONObject(map);
        JsonObjectRequest request = new JsonObjectRequest(Method.POST,
                URL_CLASS_UPDATE, jsonObject, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    public JsonObjectRequest studentListRequest(String classId,
                                                Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_STUDENT_LIST + "/" + classId, null, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }

    /**
     * 验证学生用户名
     *
     * @param userName
     * @param listener
     * @param errorListener
     * @return
     */
    public JsonObjectRequest verifyUserRequest(String userName,
                                               Listener<JSONObject> listener, ErrorListener errorListener) {

        try {
            userName = URLEncoder.encode(userName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_STUDENT_NAME + "/" + userName, null, listener,
                errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }


    /**
     * 添加学生的请求
     *
     * @param classId       班级id
     * @param students      学生实体类
     * @param listener      监听
     * @param errorListener 失败监听
     * @return
     */
    public JsonObjectRequest addStudentRequest(String classId,
                                               List<Student> students, Listener<JSONObject> listener,
                                               ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {

            Gson gson = new Gson();
            String studentsJson = gson.toJson(students);
            JSONArray sJsonArray = new JSONArray(studentsJson);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("classId", classId);
            jsonObject.put("studentInfos", sJsonArray);
            request = new JsonObjectRequest(Method.POST, URL_CLASS_STUDENT_ADD,
                    jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 删除学生的请求
     *
     * @param classId       班级id
     * @param studentIds    学生ids
     * @param listener      监听
     * @param errorListener 失败监听
     * @return
     */
    public JsonObjectRequest deleteStudentRequest(String classId,
                                                  List<String> studentIds, Listener<JSONObject> listener,
                                                  ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            Gson gson = new Gson();
            String studentsJson = gson.toJson(studentIds);
            JSONArray studentIdsJsonArray = new JSONArray(studentsJson);

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("classId", classId);
            jsonObject.put("studentIds", studentIdsJsonArray);
            request = new JsonObjectRequest(Method.POST,
                    URL_CLASS_STUDENT_DELETE, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 编辑学生的请求
     *
     * @param students      学生实体类
     * @param listener      监听
     * @param errorListener 失败监听
     * @return
     */
    public JsonObjectRequest updateStudentRequest(String classId,
                                                  List<Student> students, Listener<JSONObject> listener,
                                                  ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            Gson gson = new Gson();
            String studentJson = gson.toJson(students);
            JSONArray studentsArray = new JSONArray(studentJson);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(Constant.STUDENTINFOS, studentsArray);
            jsonObject.put("classId", classId);
            request = new JsonObjectRequest(Method.POST,
                    URL_CLASS_STUDENT_UPDATE, jsonObject, listener,
                    errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 加入班级请求(对象：所有)
     *
     * @param classId       班号
     * @param studentNo     学号
     * @param listener      结果监听
     * @param errorListener 异常监听
     * @return
     */
    public JsonObjectRequest joinClassRequest(String classId, String studentNo, Listener<JSONObject> listener,
                                              ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("classId", classId);
            map.put("studentNo", studentNo);
            JSONObject jsonObject = new JSONObject(map);
            request = new JsonObjectRequest(Method.POST,
                    URL_CLASS_STUDENT_JOIN2, jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
            request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                    DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;
    }

    /**
     * 加入班级请求(对象：所有)
     *
     * @param classId       班号
     * @param studentNo     学号
     * @param studentName   学生姓名
     * @param listener      结果监听
     * @param errorListener 异常监听
     * @return
     */
    public JsonObjectRequest joinClassRequest(String classId, String studentNo,
                                              String studentName, Listener<JSONObject> listener,
                                              ErrorListener errorListener) {
        JsonObjectRequest request = null;
        try {
            Map<String, String> map = new HashMap<String, String>();
            map.put("classId", classId);
            map.put("studentNo", studentNo);
            map.put("studentName", studentName);
            JSONObject jsonObject = new JSONObject(map);
            request = new JsonObjectRequest(Method.POST,
                    URL_CLASS_STUDENT_JOIN, jsonObject, listener, errorListener) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return addRequestHeader();
                }
            };
            request.setRetryPolicy(new DefaultRetryPolicy(DEFAULT_TIMEOUT_MS,
                    DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return request;
    }

    public JsonObjectRequest followClassRequest(String shareNo,
                                                Listener<JSONObject> listener, ErrorListener errorListener) {
        JsonObjectRequest request = new JsonObjectRequest(Method.GET,
                URL_CLASS_FOLLOW + "/" + shareNo, null, listener, errorListener) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                return addRequestHeader();
            }
        };
        return request;
    }
}